package com.zenith.edu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zenith.edu.mapper.EduCourseSelectionMapper;
import com.zenith.edu.po.domain.EduCourseSelection;
import com.zenith.edu.po.dto.EduCourseSelectionDTO;
import com.zenith.edu.po.vo.EduCourseSelectionVO;
import com.zenith.edu.service.EduCourseSelectionService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class EduCourseSelectionServiceImpl extends ServiceImpl<EduCourseSelectionMapper, EduCourseSelection> implements EduCourseSelectionService {
    
    @Override
    public EduCourseSelectionVO createCourseSelection(EduCourseSelectionDTO dto) {
        EduCourseSelection courseSelection = new EduCourseSelection();
        BeanUtils.copyProperties(dto, courseSelection);
        this.save(courseSelection);
        
        EduCourseSelectionVO vo = new EduCourseSelectionVO();
        BeanUtils.copyProperties(courseSelection, vo);
        return vo;
    }
    
    @Override
    public EduCourseSelectionVO getCourseSelectionById(Long id) {
        EduCourseSelection courseSelection = this.getById(id);
        if (courseSelection == null) {
            return null;
        }
        
        EduCourseSelectionVO vo = new EduCourseSelectionVO();
        BeanUtils.copyProperties(courseSelection, vo);
        return vo;
    }
    
    @Override
    public EduCourseSelectionVO updateCourseSelection(EduCourseSelectionDTO dto) {
        EduCourseSelection courseSelection = new EduCourseSelection();
        BeanUtils.copyProperties(dto, courseSelection);
        this.updateById(courseSelection);
        
        EduCourseSelectionVO vo = new EduCourseSelectionVO();
        BeanUtils.copyProperties(courseSelection, vo);
        return vo;
    }
    
    @Override
    public boolean deleteCourseSelection(Long id) {
        return this.removeById(id);
    }
    
    @Override
    public List<EduCourseSelectionVO> getCourseSelectionList() {
        List<EduCourseSelection> courseSelectionList = this.list();
        return courseSelectionList.stream().map(courseSelection -> {
            EduCourseSelectionVO vo = new EduCourseSelectionVO();
            BeanUtils.copyProperties(courseSelection, vo);
            return vo;
        }).collect(Collectors.toList());
    }
}